$(function () {
  console.log('dom已经准备妥当')

  $('#btnQuery').click(function () {
    console.log('你进行了一次查找')
    let keyword = $('[name=keyword]').val()
    let obj = {
      keyword,
    }
    $.post('/account/query', obj, function (res) {
      console.log(res);
      $('.tbRow').html('')
      res.forEach(item=>{
        let html=`
        <tr class="tbRow">
            <td>${item.id}</td>
            <td>${item.account}</td>
            <td>${item.balance}</td>
            <td>${item.isActived}</td>
            <td>${item.updatedTime}</td>
            <td>${item.remarks}</td>
            <td>
                <input class="btnEdit" onclick="btnEdit(${item.id})" type="button" value="编辑">
                <input class="btnDel" onclick="btnDel(${item.id})" type="button" value="删除">
            </td>
        </tr>
        `
        
        $('#tbData').append(html)
      })
    })
  })

  $('#btnAdd').click(function () {
    window.location.href = `/account/createOrEdit`
  })

  // $('.btnEdit').click(function(id){
  //     window.location.href=`/account/createOrEdit/${id}`
  // })
  // $('.btnDel').click(function(){
  //     console.log('你进行了一次删除');
  // })

  $('#btnSave').click(function () {
    let id = $('[name=id]').val()
    let account = $('[name=account]').val()
    let balance = $('[name=balance]').val()
    let isActive = $('[name=isActive]').val()
    let remarks = $('[name=remarks]').val()
    let obj = {
      id,
      account: account,
      balance,
      isActive,
      remarks,
    }
    console.log(obj)
    // $.ajax({
    //     type:'post',
    //     url:'/account/save',
    //     dataType: "json",
    //     data:obj,
    //     success:function(){

    //     }
    // })
    $.post('/account/save', obj, function () {
      alert('保存成功！')
      window.location.href = `/account/index`
    })
  })
  $('#btnCancel').click(function () {
    window.location.href = `/account/index`
  })
})
function btnEdit(id) {
  window.location.href = `/account/createOrEdit/${id}`
}

function btnDel(id) {
  let msg = '你真的想删除我吗？'
  if (confirm(msg)) {
    window.location.href = `/account/Delete/${id}`
  } else {
    console.log('用户取消了删除动作')
  }
}
